package com.smart.wechat.login.common;


import lombok.Data;

import java.io.Serializable;

/**
 * 响应信息主体
 * @author xuedi
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> success() {
        return restResult(null, ResponseCodeEnum.SUCCESS.getCode(), null);
    }

    public static <T> R<T> success(T data) {
        return restResult(data, ResponseCodeEnum.SUCCESS.getCode(), null);
    }

    public static <T> R<T> success(T data, String msg) {
        return restResult(data, ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> success(String msg) {
        return restResult(null, ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ResponseCodeEnum.FAIL.getCode(), null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ResponseCodeEnum.FAIL.getCode(), msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ResponseCodeEnum.FAIL.getCode(), null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ResponseCodeEnum.FAIL.getCode(), msg);
    }

    public static <T> R<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<T>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }


}
